package bb.lanxing.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import bb.lanxing.R;
import bb.lanxing.lib.widget.LockableViewPager;
import bb.lanxing.util.image.GlideUtils;

public class PhotoViewPager extends RelativeLayout {
    private ImageLoadingListener imageLoadingListener;
    private PageAdapter mAdapter;
    private int mDefaultDrawableRes;
    private float mDensity;
    private LinearLayout mDotsLayout;
    private int mDotsNum;
    private DisplayImageOptions mImageOptions;
    private ViewPager.OnPageChangeListener mListener;
    private OnItemClickListener mOnItemClickListener;
    private LockableViewPager mViewPager;
    private boolean mZoomable;

    public interface OnItemClickListener {
        void onItemClick(int i);

        void onItemLongClick(int i);

        void onPhotoTap(int i);
    }

    public PhotoViewPager(Context context) {
        this(context, null);
    }

    public PhotoViewPager(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public PhotoViewPager(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mDensity = -1.0f;
        this.mDotsNum = 0;
        this.mZoomable = false;
        this.mDefaultDrawableRes = R.drawable.bg_photo_default;
        this.mListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int i2) {
            }

            @Override
            public void onPageScrolled(int i2, float f, int i3) {
            }

            @Override
            public void onPageSelected(int i2) {
                if (PhotoViewPager.this.mDotsNum <= 1 || i2 >= PhotoViewPager.this.mDotsNum) {
                    return;
                }
                PhotoViewPager.this.setDotsEnable(i2);
            }
        };
    }

    public void init(Context context, String[] strArr, boolean z) {
        this.mDotsNum = strArr.length;
        this.mZoomable = z;
        this.mImageOptions = new DisplayImageOptions.Builder().showImageOnLoading(this.mDefaultDrawableRes).showImageForEmptyUri(this.mDefaultDrawableRes).showImageOnFail(this.mDefaultDrawableRes).cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).build();
        this.mAdapter = new PageAdapter(context, strArr);
        LockableViewPager lockableViewPager = new LockableViewPager(context);
        this.mViewPager = lockableViewPager;
        lockableViewPager.addOnPageChangeListener(this.mListener);
        this.mViewPager.setAdapter(this.mAdapter);
        this.mViewPager.setLayoutParams(new LayoutParams(-1, -1));
        addView(this.mViewPager);
        if (this.mDotsNum > 1) {
            this.mDotsLayout = new LinearLayout(context);
            LayoutParams layoutParams = new LayoutParams(-2, -2);
            layoutParams.addRule(10);
            layoutParams.addRule(14);
            this.mDotsLayout.setLayoutParams(layoutParams);
            int i = 0;
            while (i < this.mDotsNum) {
                ImageView imageView = new ImageView(context);
                imageView.setImageResource(R.drawable.intro_dot_bg);
                imageView.setEnabled(i == 0);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams((int) dip2px(6.0f), (int) dip2px(6.0f));
                layoutParams2.setMargins(8, 16, 8, 0);
                this.mDotsLayout.addView(imageView, layoutParams2);
                i++;
            }
            addView(this.mDotsLayout);
        }
    }

    public void setCurIndex(int i) {
        LockableViewPager lockableViewPager = this.mViewPager;
        if (lockableViewPager != null) {
            lockableViewPager.setCurrentItem(i);
        }
    }

    public int getCurIndex() {
        LockableViewPager lockableViewPager = this.mViewPager;
        if (lockableViewPager != null) {
            return lockableViewPager.getCurrentItem();
        }
        return 0;
    }

    public void setDefaultDrawable(int i) {
        this.mDefaultDrawableRes = i;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setDotsEnable(int i) {
        if (this.mDotsLayout != null) {
            int i2 = 0;
            while (i2 < this.mDotsNum) {
                this.mDotsLayout.getChildAt(i2).setEnabled(i2 == i);
                i2++;
            }
        }
    }

    private float dip2px(float f) {
        if (this.mDensity == -1.0f) {
            this.mDensity = getResources().getDisplayMetrics().density;
        }
        return (f * this.mDensity) + 0.5f;
    }

    public class PageAdapter extends PagerAdapter {
        private Context mContext;
        private String[] mPhotoUrls;

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        public PageAdapter(Context context, String[] strArr) {
            this.mContext = context;
            this.mPhotoUrls = strArr;
        }

        @Override
        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            viewGroup.removeView((View) obj);
        }

        @Override
        public int getCount() {
            return PhotoViewPager.this.mDotsNum;
        }

        @Override
        public Object instantiateItem(ViewGroup viewGroup, final int i) {
            ImageView photoView = PhotoViewPager.this.mZoomable ? new PhotoView(this.mContext) : new ImageView(this.mContext);
            if (Build.VERSION.SDK_INT >= 21) {
                photoView.setTransitionName("imageView" + i);
            }
            photoView.setScaleType(PhotoViewPager.this.mZoomable ? ImageView.ScaleType.FIT_CENTER : ImageView.ScaleType.CENTER_CROP);
            if (PhotoViewPager.this.mZoomable) {
                ((PhotoView) photoView).setOnPhotoTapListener(new OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(ImageView view, float x, float y) {
                        if (PhotoViewPager.this.mOnItemClickListener != null) {
                            PhotoViewPager.this.mOnItemClickListener.onPhotoTap(i);
                        }

                    }
                });
            }
            photoView.setOnClickListener(view -> {
                if (PhotoViewPager.this.mOnItemClickListener != null) {
                    PhotoViewPager.this.mOnItemClickListener.onItemClick(i);
                }
            });
            photoView.setOnLongClickListener(view -> {
                if (PhotoViewPager.this.mOnItemClickListener != null) {
                    PhotoViewPager.this.mOnItemClickListener.onItemLongClick(i);
                    return true;
                }
                return true;
            });
            if (i < this.mPhotoUrls.length) {
                if (PhotoViewPager.this.mZoomable) {
                    GlideUtils.companion.loadImage(photoView.getContext(), GlideUtils.companion.appendImagePath(this.mPhotoUrls[i], 6), photoView, R.drawable.bg_photo_default, R.drawable.bg_photo_default);
                } else {
                    GlideUtils.companion.loadImage(photoView.getContext(), GlideUtils.companion.appendImagePath(this.mPhotoUrls[i], 5), photoView, R.drawable.bg_photo_default, R.drawable.bg_photo_default);
                }
            }
            viewGroup.addView(photoView, -1, -1);
            return photoView;
        }
    }

    public ImageLoadingListener getImageLoadingListener() {
        return this.imageLoadingListener;
    }

    public void setImageLoadingListener(ImageLoadingListener imageLoadingListener) {
        this.imageLoadingListener = imageLoadingListener;
    }
}
